<?php
declare (strict_types=1);

namespace app\topic\logic\admin;

use app\admin\logic\system\Log;
use app\common\exception\LogicException;
use app\topic\model\admin\Topic as TopicModel;
use think\db\exception\DbException;
use think\Paginator;

/**
 * 资讯逻辑层
 */
class Topic extends TopicModel
{
    /**
     * 获取列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-01
     */
    public function getList(array $map = []): Paginator
    {
        return $this->with(['coverUrl', 'school'])->where($map)->order('update_time', 'DESC')->paginate(static::getListRows());
    }

    /**
     * 创建
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-01
     *
     * @param array $data [description]
     */
    public function createTopic(array $data): Topic
    {
        $save = [
            'title'            => $data['title'],
            'fullcategorypath' => $data['cates'],
            'category'         => $data['cates'],
            'cover'            => $data['cover'],
            'abstract'         => $data['abstract'],
            'content'          => $data['content'],
            'source'           => $data['source'] ?? '',
            'best_sort'        => $data['best_sort'] ?? 999,
        ];

        // 验证数据
        $this->getValidate()->scene('create')->failException()->check($save);
        // 创建成功
        if (!$this->save($save)) {
            throw new LogicException('创建失败');
        }

        Log::write('新建资讯【' . $data['title'] . '】', 'topic', 'create', true);

        // 返回值
        return $this;
    }

    /**
     * 编辑
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-01
     *
     * @param    [type]                         $id   [description]
     * @param    [type]                         $data [description]
     */
    public function editTopic($id, $data)
    {
        $topic = $this->where('id', $id)->findOrFail();

        $check                     = $data;
        $check['id']               = $id;
        $check['fullcategorypath'] = $data['cates'];
        $check['category']         = $data['cates'];
        // 验证数据
        $this->getValidate()->scene('edit')->failException()->check($check);

        $topic->title            = $data['title'];
        $topic->fullcategorypath = $data['cates'];
        $topic->category         = $data['cates'];
        $topic->cover            = $data['cover'];
        $topic->abstract         = $data['abstract'];
        $topic->content          = $data['content'];
        $topic->source           = $data['source'] ?? '';
        $topic->best_sort        = $data['best_sort'] ?? 999;

        if (!$topic->save()) {
            throw new LogicException('更新失败');
        }

        Log::write('更新资讯【' . $data['title'] . '】', 'topic', 'update', true);

        return $topic;
    }

    /**
     * 删除
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-04
     *
     * @param    [type]                         $id [description]
     */
    public function deleteTopic($id)
    {
        $topic = $this->where('id', $id)->findOrFail();

        if (!$topic->delete()) {
            throw new LogicException('删除失败');
        }

        Log::write('删除资讯【' . $topic->title . '】', 'topic', 'delete', true);
    }

    /**
     * 更新某个值
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/11/18
     *
     * @param int    $id
     * @param string $field
     * @param        $value
     */
    public function editField(int $id, string $field, $value)
    {
        // 信息
        $topic = $this->findOrFail($id);

        $topic->$field = $value;

        if (!$topic->force()->save()) {
            throw new LogicException('更新失败');
        }

        Log::write('编辑资讯【' . $topic->title . '】排序', 'topic_order', 'update', true);
    }

    /**
     * 资讯列表
     *
     * @param       $keyword
     * @param       $count
     * @param array $map
     * @return Paginator
     * @throws \think\db\exception\DbException
     */
    public function getTopicList($keyword, $count, array $map)
    {
        if ($keyword) {
            $map[] = ['title', 'like', '%' . $keyword . '%'];
        }

        return $this->where($map)->with(['coverUrl'])->paginate($count);
    }

    /**
     * 资讯详情
     *
     * @param array $ids
     * @return Topic[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTopics(array $ids)
    {
        $datas = $this->with(['coverUrl'])->whereIn('id', $ids)->order('update_time', 'DESC')->select();

        return $datas;
    }

    /**
     * 批量删除
     *
     * @param $ids
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function deletes($ids): void
    {
        $list = $this->whereIn('id', $ids)->select();
        if (!$list->delete()) {
            throw new LogicException('操作失败');
        }
    }
}
